home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / text / misc / strip11.lzh / Strip.c < prev    next >
C/C++ Source or Header  |  1995-08-27  |  2KB  |  105 lines

  1. /***************************
  2.  * Strip $Revision: 1.1 $ *
  3.  * (C) 1995 Simon Austin *
  4.  ***************************/
  5.  
  6. /* $Id: Strip.c,v 1.1 1995/03/23 16:32:16 simon Exp simon $ */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12.  
  13. char *version="$VER: Strip (C)1995 S Austin $Revision: 1.1 $";
  14. char *errorcl="Usage: Strip [Infile] [Outfile]\n";
  15. char infilename[50], outfilename[50];
  16. char inchar, outchar;
  17. FILE *infile, *outfile;
  18.  
  19. void filefail(char *);
  20.  
  21. main(int argc, char *argv[])
  22. {
  23.   if(argc < 1 || argc > 3)
  24.   {
  25.     /* between none and two options are allowed on the command line */
  26.     fputs(errorcl, stderr);
  27.     exit(0);
  28.   }
  29.   
  30.   /* Set filenames to - so uses stdin/stdout unless redirected */
  31.   strcpy(infilename, "-");
  32.   strcpy(outfilename, "-"); 
  33.   
  34.   if(argc == 2)
  35.   {
  36.     /* One option: get name of infile */
  37.     strcpy(infilename, argv[1]);
  38.   }
  39.   
  40.   if(argc == 3)
  41.   {
  42.     /* Two options: get names of in and out files */
  43.     strcpy(infilename, argv[1]);
  44.     strcpy(outfilename, argv[2]);
  45.     if(!strcmp(infilename, outfilename) && strcmp(infilename, "-"))
  46.       strcat(outfilename, ".str");
  47.   }
  48.   
  49.   /* Set file pointers to stdin/stdout. They'll be set otherwise if required */
  50.   infile = stdin;
  51.   outfile = stdout;
  52.   
  53.   /* If the input filename is not -, try and open the file */
  54.   if(strcmp(infilename, "-"))
  55.   {
  56.     infile = fopen(infilename, "r");
  57.     if(!infile)
  58.     {
  59.       filefail(infilename);
  60.     }
  61.   }
  62.  
  63.   if(strcmp(outfilename, "-"))
  64.   {
  65.     outfile = fopen(outfilename, "w");
  66.     if(!outfile)
  67.     {
  68.       filefail(outfilename);
  69.     }
  70.   }
  71.   
  72.   outchar = ' ';
  73.   inchar = fgetc(infile);
  74.   
  75.   while(inchar != EOF && outchar != EOF)
  76.   {
  77.     if(isprint(inchar))
  78.       outchar = fputc(inchar, outfile);
  79.     if(inchar == '\n' || inchar == '\t')
  80.       outchar = fputc(inchar, outfile);
  81.     inchar = fgetc(infile);
  82.   }
  83.   
  84.   if(outchar == EOF)
  85.   {
  86.     fputs("Error in \"", stderr);
  87.     fputs(outfilename, stderr);
  88.     fputs("\".\n", stderr);
  89.   }
  90.   
  91.   if(strcmp(infilename, "-"))
  92.     fclose(infile);
  93.   if(strcmp(outfilename, "-"))
  94.     fclose(outfile);
  95.  
  96.   exit(0);
  97. }
  98.  
  99. void filefail(char *filename)
  100. {
  101.   fputs("Cannot open \"", stderr);
  102.   fputs(filename, stderr);
  103.   fputs("\".\n", stderr);
  104.   exit(0);
  105. }